package com.imooc.sm.controller;

import com.imooc.sm.entity.Staff;
import com.imooc.sm.service.PowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller("powerController")
public class PowerController {

    @Autowired
    private PowerService powerService;

    // 功能权限 power/toText.do
    public void toText(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("../power_text.jsp").forward(request,response);
    }

    // 角色权限 power/list.do
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Staff> list = powerService.getAll();
        request.setAttribute("rList", list);
        request.getRequestDispatcher("../power_rode.jsp").forward(request,response);
    }

    // power/toEdit.do
    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        Staff staff = powerService.get(id);
        /*为了把旧权限显示在更改权限界面*/
        request.setAttribute("RS",staff);
        request.getRequestDispatcher("../power_edit.jsp").forward(request,response);
    }

    // edit.do
    public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        String newRole = request.getParameter("newRole");
        Staff staff = new Staff();
        staff.setId(id);
        staff.setStatus(newRole);
        powerService.edit(staff);
        response.sendRedirect("list.do");
    }
}
